/**
 * Copyright ${license.git.copyrightYears} the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hong.helloWorld;

import hong.helloWorld.orm.entity.cms.Context;
import hong.helloWorld.orm.entity.upms.User;
import hong.helloWorld.plugin.PageHelper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.Reader;
import java.util.Date;
import java.util.List;

public class Main {


    public  SqlSession getSqlSession() throws Exception{
        String resource = "hong/helloWorld/Configuration.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = sqlSessionFactory.openSession();

        return  session;
    }

    @Test
    public void selectAllUser1() throws Exception {

        SqlSession sqlSession1 = getSqlSession();

        PageHelper.startPage(0,100);
        List<User> userList2323 = sqlSession1.selectList("hong.helloWorld.orm.dao.upms.UserMapper.selectAll");

        sqlSession1.close();
//


    }



    @Test
    public void selectAllUser() throws Exception {
        String resource = "hong/helloWorld/Configuration.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        List<User> userList2323 = sqlSession1.selectList("hong.helloWorld.orm.dao.upms.UserMapper.selectAll");
          userList2323 = sqlSession1.selectList("hong.helloWorld.orm.dao.upms.UserMapper.selectAll");

        sqlSession1.close();
//
//
//        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE,false);
//        try {
//            List<User> userList = sqlSession.selectList("hong.helloWorld.orm.dao.upms.UserMapper.selectAll");
//            User user = new User();
//            user.setId(new Date().getTime()+"");
//            user.setName("xiaowang");
//            user.setAge(10);
//            user.setBirthday(new Date());
//            sqlSession.insert("hong.helloWorld.orm.dao.upms.UserMapper.insert", user);
//            List<User> userList1 = sqlSession.selectList("hong.helloWorld.orm.dao.upms.UserMapper.selectAll");
//
//
//            int i= 1/0;
//            System.out.println("fsfdsfsdfdsf");
//
//
////            sqlSession.commit();
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            sqlSession.rollback();
//
//        }finally {
//
//
//            sqlSession.close();
//        }
//
//


//


    }

    @Test
    public void insertUser() throws Exception {
        SqlSession sqlSession = getSqlSession();


        User user = new User();
        user.setId(new Date().getTime()+"");
        user.setName("xiaowang");
        user.setAge(10);
        user.setBirthday(new Date());
        int res = sqlSession.insert("hong.helloWorld.orm.dao.upms.UserMapper.insert", user);
        System.out.println(res==1?"insert User success":"insert User error");
        sqlSession.commit();
        sqlSession.close();


    }


    @Test
    public void selectAllContext() throws Exception {

        SqlSession sqlSession = getSqlSession();
        List<Context> userList = sqlSession.selectList("hong.helloWorld.orm.dao.cms.ContextMapper.selectAll");
        for (Context context:userList) {
            System.out.println(context);
        }
        sqlSession.close();
    }

    @Test
    public void insertContext() throws Exception {
        SqlSession sqlSession = getSqlSession();

        Context context = new Context();
        context.setId(new Date().getTime()+"");
        context.setUserId("20002");
        context.setTitle("title");
        context.setContent("context");
        context.setCreateTime(new Date());

        int res = sqlSession.insert("hong.helloWorld.orm.dao.cms.ContextMapper.insert", context);
        System.out.println(res==1?"insert Context success":"insert Context error");
        sqlSession.commit();
        sqlSession.close();


    }

    @Test
    public void updateContextById() throws Exception {
        SqlSession sqlSession = getSqlSession();

        Context context = new Context();
        context.setId("1545920033746");
        context.setUserId("20837");
        context.setTitle("title1");
        context.setContent("context1");
        context.setCreateTime(new Date());

        int res = sqlSession.insert("hong.helloWorld.orm.dao.cms.ContextMapper.updateById", context);
        System.out.println(res==1?"update Context success":"update Context error");
        sqlSession.commit();
        sqlSession.close();


    }

}
